// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MockEventArgs.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Test.Robotics.Hardware.DAC
{
    using System;
    using System.Timers;

    using Microsoft.Robotics.Hardware.DAC;

    /// <summary>
    /// An AsyncEventArgs implementation that will complete on a schedule
    /// </summary>
    internal class MockEventArgs : AsyncEventArgs, IDisposable
    {
        /// <summary>
        /// A timer that controls completion
        /// </summary>
        private Timer timer;

        /// <summary>
        /// The buffer to be used upon completion
        /// </summary>
        private byte[] buffer;

        /// <summary>
        /// Initializes a new instance of the <see cref="MockEventArgs" /> class.
        /// </summary>
        /// <param name="transport">The transport that will be used</param>
        /// <param name="buffer">A buffer containing the data that will be used on completion</param>
        public MockEventArgs(ITransport transport, byte[] buffer)
            : base(transport, buffer)
        {
            this.timer = new Timer(1000);
            this.timer.Elapsed += new ElapsedEventHandler(this.OnTime);
            this.buffer = (byte[])buffer.Clone();
        }

        /// <summary>
        /// Begins the cycle of scheduled completing
        /// </summary>
        public void On()
        {
            this.timer.Start();
        }

        /// <summary>
        /// Stops the EventArgs from completing 
        /// </summary>
        public void Off()
        {
            this.timer.Stop();
        }

        /// <summary>
        /// Implement the IDisposable interface
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Dispose of resources
        /// </summary>
        /// <param name="disposing">Indicates whether or not Dispose() was called.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.timer.Dispose();
            }
        }

        /// <summary>
        /// Handles timer ticks by completing the event
        /// </summary>
        /// <param name="sender">The parameter is not used.</param>
        /// <param name="e">The parameter is not used.</param>
        private void OnTime(object sender, ElapsedEventArgs e)
        {
            this.BytesTransferred = this.buffer.Length;
            this.OnCompleted();
        }
    }
}
